package com.xzit.rental.security;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.security.SecureRandom;

@Configuration
@Data
public class PasswordConfig {

    @Value("${encoder.ctype.strength}")
    private int strength;
    @Value("${encoder.ctype.secret}")
    private String secret;

    // 强散列密码解析器
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        SecureRandom secureRandom = new SecureRandom(secret.getBytes());
        return new BCryptPasswordEncoder(strength, secureRandom);
    }

    /*public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new PasswordConfig().bCryptPasswordEncoder();
        System.out.println(passwordEncoder.encode("123"));
        System.out.println(passwordEncoder.encode("123"));

        boolean b = passwordEncoder.matches("123", "$2a$06$tKkMLHXCk/zytzBGB2fdAOa7nrpTbMkjYd.rqRJPEg2lSzI3QA/Ym");
        System.out.println(b);
    }*/

}
